package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class Register {
	
	private nl.hu.verbrandendephoenix.view.Register registerForm;
	
	public Register(RootPanel pnlContent) { }
	
	public Panel getPanel() {
		registerForm = new nl.hu.verbrandendephoenix.view.Register();
		Panel p = registerForm.constructForm();
		addHandlers();
		return p;
	}
	private void processSubmit() {
		final NotificationManager nM =  NotificationManager.get();
		
		if(!checkFields()) {
			nM.createNotification("Er is iets mis gegaan!");
		} else {
			AppManager.get().getRpcService().createUser(registerForm.getTxtUsername().getText(), registerForm.getTxtPassword().getText(), registerForm.getTxtEmail().getText(), new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					nM.commError();
				}
				public void onSuccess(Boolean result) {
					if(result) {
						nM.createNotification("U bent succesvol geregistreerd.");
					} else {
						nM.createNotification("U heeft niet alle velden correct ingevuld of gebruikersnaam bestaat al.");
					}
				}
			});
		}
	}
	/**
	 * checkFields() checkt of alle velden goed zijn ingevuld.
	 * @return boolean true wanneer goed ingevuld, false wanneer fout.
	 */
	private boolean checkFields() {
		FieldVerifier fv = FieldVerifier.get();
		boolean b = true; 
		
		TextBox txtUsername = registerForm.getTxtUsername();
		if(!fv.checkString(4, 30, txtUsername.getText())) {
			fv.addRequired(txtUsername);
			b = false;
		} else {
			fv.removeRequired(txtUsername);
		}
		
		TextBox txtPassword = registerForm.getTxtPassword();
		if(!fv.checkString(5, 50, txtPassword.getText())) {
			fv.addRequired(txtPassword);
			b = false;
		} else {
			fv.removeRequired(txtPassword);
		}
		
		TextBox txtPasswordRepeat = registerForm.getTxtPasswordRepeat();
		if(!fv.checkString(5, 50, txtPasswordRepeat.getText()) ||	
			!txtPassword.getText().equals(txtPasswordRepeat.getText())) 
		{
			fv.addRequired(txtPasswordRepeat);
			b = false;
		} else {
			fv.removeRequired(txtPasswordRepeat);
		}
		
		TextBox txtEmail = registerForm.getTxtEmail();
		if(!fv.checkEmailAddress(txtEmail.getText())) {
			fv.addRequired(txtEmail);
			b = false;
		} else {
			fv.removeRequired(txtEmail);
		}
		
		return b;
	}
	/**
	 * click & key handler, deze reageren op de ENTER toets en de submit knop.
	 */
	public void addHandlers() {
		Button btnRegister = registerForm.getBtnRegister();
		btnRegister.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				processSubmit();
			} 
		});
		
		ArrayList<TextBox> arrTextBoxes = registerForm.getArrTextBox();
		for(TextBox tb : arrTextBoxes) {
			tb.addKeyPressHandler(new KeyPressHandler() {
				@Override
				public void onKeyPress(KeyPressEvent event) {
					if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
						processSubmit();
					}
				}
			});
		}
		
	}
}
